package com.example.mybatisplusdemo.common;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mybatisplusdemo.model.domain.Flight;
import com.example.mybatisplusdemo.model.domain.ItineraryRequest;
import com.example.mybatisplusdemo.model.domain.ItineraryRequest.TimeFilter;

import java.time.LocalTime;
import java.util.List;

public class AdvancedFlightQueryWrapper extends FlightQueryWrapper {

    // 增强的出发/到达时间过滤
    public AdvancedFlightQueryWrapper applyEnhancedTimeFilters(
            List<TimeFilter> filters,
            boolean isDeparture,
            String timePreference) {
        if (filters == null || filters.isEmpty()) {
            // 默认时间偏好处理
            if ("morning".equals(timePreference)) {
                return applyDefaultTimeRange(isDeparture,
                        LocalTime.of(6, 0), LocalTime.of(11, 59));
            } else if ("afternoon".equals(timePreference)) {
                return applyDefaultTimeRange(isDeparture,
                        LocalTime.of(12, 0), LocalTime.of(17, 59));
            } else if ("evening".equals(timePreference)) {
                return applyDefaultTimeRange(isDeparture,
                        LocalTime.of(18, 0), LocalTime.of(23, 59));
            }
            return this;
        }
        return (AdvancedFlightQueryWrapper) super.applyTimeFilters(filters, isDeparture);
    }

    private AdvancedFlightQueryWrapper applyDefaultTimeRange(
            boolean isDeparture, LocalTime start, LocalTime end) {
        String column = isDeparture ? "departure_time" : "arrival_time";
        return (AdvancedFlightQueryWrapper) this
                .ge(column, start)
                .le(column, end);
    }

    // 增强的联盟过滤
    public AdvancedFlightQueryWrapper applyAllianceFilters(
            List<String> allianceCodes, List<String> airlines) {
        if (allianceCodes != null && !allianceCodes.isEmpty()) {
            // 实际项目中这里应该查询联盟对应的航空公司
            // 这里简化为直接使用传入的航空公司列表
            if (airlines != null) {
                this.in("airline_code", airlines);
            }
        }
        return this;
    }

    // 增强的优惠活动过滤
    public AdvancedFlightQueryWrapper applyPromotionFilters(
            Boolean hasDiscount, Boolean memberOnly) {
        if (hasDiscount != null && hasDiscount) {
            // 实际项目中这里应该关联优惠表查询
            // 这里简化为存在优惠标记的航班
            this.isNotNull("promotion_tag");
        }
        return this;
    }
}